package com.ikun.controller;

import com.ikun.pojo.Result;
import com.ikun.pojo.TbUser;
import com.ikun.pojo.TbUserPrize;
import com.ikun.service.AlipayService;
import com.ikun.service.UserPrizeService;
import com.ikun.service.UserService;
import com.ikun.util.TimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.List;

@Controller
public class UserPrizeController {
    @Autowired
    UserService userService;
    @Autowired
    UserPrizeService userPrizeServiceimpl;
    @ResponseBody
    @RequestMapping("cashrewards")//兑换奖励 用户id 商品id 商品价格
    public Result cashRewards(Long userId,Long prizeid, BigDecimal money,HttpServletRequest request){
        System.out.println(userId+":"+prizeid+":"+money);
        Result result = new Result();
        result.setCode(1);
        int count = userService.deductMoney(userId,money);//扣钱
        if(count>0){
            //获取session中的用户
            TbUser tbUserNow = (TbUser) request.getSession().getAttribute("tbUserNow");
            BigDecimal a= tbUserNow.getBalance();
            BigDecimal b = a.subtract(money);//减法
            tbUserNow.setBalance(b);
            request.getSession().setAttribute("tbUserNow",tbUserNow);//设置新的用户到seesion
            int count1 = userPrizeServiceimpl.cashRewards(userId,prizeid);//提交申请
            if (count1>0){
                result.setCode(0);
            }
        }
        return result;//兑换
    }
    @RequestMapping("selectmsg")
    @ResponseBody
    public Result selectmsg(HttpServletRequest request, HttpServletResponse response){
        List<TbUserPrize> list=userPrizeServiceimpl.selectmsg();
        Result result=new Result();
        if (list!=null){
            result.setCode(0);
            result.setMessage("有数据");
            result.setData(list);
            System.out.print(list);
            return result;
        }else {
            result.setCode(0);
            System.out.print("查询出错");
        }
        return  result;
    }
    @RequestMapping("selectprize")
    @ResponseBody
    public Result selectprize(HttpServletRequest request, HttpServletResponse response){
        List<TbUserPrize> list=userPrizeServiceimpl.selectprize();
        Result result=new Result();
        if (list!=null){
            result.setCode(0);
            result.setMessage("有数据");
            result.setData(list);
            System.out.print(list);
            return result;
        }else {
            result.setCode(0);
            System.out.print("查询出错");
        }
        return  result;
    }
    @RequestMapping("check")
    @ResponseBody
    public Result check(HttpServletRequest request, HttpServletResponse response){
        int id=Integer.parseInt(request.getParameter("id"));
        String finishtime= TimeUtil.getNowTime();
        int count=userPrizeServiceimpl.check(id,finishtime);
        System.out.print("编号是"+id);
        System.out.print("时间是"+finishtime);
        Result result=new Result();
        if (count!=0){
            result.setCode(0);
            result.setMessage("修改成功");
            System.out.print(count);
            return result;
        }else {
            result.setCode(0);
            System.out.print("修改失败");
        }
        return  result;
    }
    @RequestMapping("delprize")
    @ResponseBody
    public Result delprize(HttpServletRequest request, HttpServletResponse response){
        int id=Integer.parseInt(request.getParameter("id"));
        int count=userPrizeServiceimpl.delprize(id);
        Result result=new Result();
        if (count!=0){
            result.setCode(0);
            result.setMessage("删除成功");
            System.out.print(count);
            return result;
        }else {
            result.setCode(0);
            System.out.print("修改失败");
        }
        return  result;
    }
}
